import { LoggerMiddleware } from './core/middlerware/logger.middleware';
import { PhotoEntity } from './entities/photo.entity';
import { UserEntity } from './entities/user.entity';
import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UserModule } from './feature/user/user.module';
import configuration from './config/configuration';
import emailConfig from './config/email';
import mysqlConfig from './config/mysql';
import { StatusMonitorModule } from 'nest-status-monitor';
import { AuthModule } from './feature/auth/auth.module';
import { EmailModule } from './feature/email/email.module';
import statusMonitorConfig from './config/statusMonitor';
import { MailerModule } from '@nest-modules/mailer';
import { GroupModule } from './feature/group/group.module';
import { FriendModule } from './feature/friend/friend.module';
import { ChatModule } from './feature/chat/chat.module';
import { UserGroupEntity } from './entities/userGroup.entity';
import { FriendEntity } from './entities/friend.entity';
import { FriendMessageEntity } from './entities/friendMessage.entity';
import { GroupEntity } from './entities/group.entity';
import { GroupMessageEntity } from './entities/groupMessage.entity';
import { ChatGateway } from './feature/chat/chat.gateway';
@Module({
  imports: [
    ConfigModule.forRoot({
      // envFilePath: '.env',
      load: [configuration, mysqlConfig, emailConfig],
      isGlobal: true,
    }),
    StatusMonitorModule.setUp(statusMonitorConfig),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        type: 'mysql',
        host: configService.get('mysql.host'),
        port: configService.get('mysql.port'),
        database: configService.get('mysql.database'),
        username: configService.get('mysql.username'),
        password: configService.get('mysql.password'),
        entities: [
          UserEntity, PhotoEntity,
          UserGroupEntity, FriendEntity, FriendMessageEntity,
          GroupEntity, GroupMessageEntity,
        ],
        synchronize: true,
      }),
      inject: [ConfigService],
    }),
    MailerModule.forRootAsync({
      useFactory: (config: ConfigService) => config.get('email'),
      inject: [ConfigService]
    }),
    UserModule,
    AuthModule,
    EmailModule,
    GroupModule,
    FriendModule,
    ChatModule
  ],
  controllers: [AppController],
  providers: [
    AppService,
    // ChatGateway
  ],
})
export class AppModule implements NestModule{
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(LoggerMiddleware).forRoutes('*')
  }
}
